<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="roleIds">
      <!-- 选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {assignRoles} from '@/api/employees'
import { getUserDetailById } from "@/api/user";
import { getRoleList } from "@/api/setting";
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
    },
    //  用户id用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      // 负责存储当前所有的角色id列表
      list: [],
      // 负责存储当前用户所拥有的角色id
      roleIds: [],
    };
  },
  created() {
   this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({page:1,pagesize:20});
      this.list = rows;
    },
    // 获取用户的当前角色
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id);
      this.roleIds = roleIds; // 赋值本用户的角色
    },
    async btnOk() {
      await assignRoles({id: this.userId,roleIds: this.roleIds})
      // 关闭弹窗
      this.$emit('update:showRoleDialog',false)
    },
   //  取消或者关闭
    btnCancel() {
      //清空原来的数组
      this.roleIds = []
      this.$emit('update:showRoleDialog',false)
    }
  },
};
</script>

<style>
</style>